package com.barryredmond.ste;

import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.TooManyListenersException;
import java.util.Vector;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;

import com.barryredmond.com.ComConnection;
import com.barryredmond.gps.GpsPosition;
import com.barryredmond.gps.GpsPositionParser;
import com.barryredmond.gps.GpsSatellite;

public class StaticTrailExplorer implements SerialPortEventListener {

	static Logger logger = Logger.getLogger(StaticTrailExplorer.class);
	
	static ComConnection GpsConnection;
	
	public static void main(String[] args){
		
		// Set up a simple configuration that logs on the console.
		BasicConfigurator.configure();
		
		logger.info("Starting application.");
	     
		new StaticTrailExplorer();
		
		logger.setLevel(Level.OFF);
		
		while(true){
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			Vector<GpsSatellite> sats = GpsPositionParser.getPositionParser().getSatellites();
			GpsPosition pos = GpsPositionParser.getPositionParser().getGpsPosition();
			
			System.out.println("SatelliteCount:" + sats.size());
			System.out.println("GPS - Pos: " + pos.toString());
		}	
	}
	

	int[] count;
	HashSet<String> strings;
	InputStream inputStream;
	GpsPositionParser parser;
	
	public StaticTrailExplorer(){
		try {
			GpsConnection = new ComConnection("COM4");
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
		try {
			GpsConnection.setSerialPortEventListener(this);
		} catch (TooManyListenersException e) {
			e.printStackTrace();
			return;
		}
		inputStream = GpsConnection.getInputStream();
		
		parser = GpsPositionParser.getPositionParser();
	}
	
	
	public void serialEvent(SerialPortEvent event) {
		switch (event.getEventType()) {
		case SerialPortEvent.BI:
		case SerialPortEvent.OE:
		case SerialPortEvent.FE:
		case SerialPortEvent.PE:
		case SerialPortEvent.CD:
		case SerialPortEvent.CTS:
		case SerialPortEvent.DSR:
		case SerialPortEvent.RI:
		case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
			break;
		case SerialPortEvent.DATA_AVAILABLE:
			StringBuffer readBuffer = new StringBuffer();
			int c;
			try {
				while ((c = inputStream.read()) != 10) {
					if (c != 13)
						readBuffer.append((char) c);
				}
				String scannedInput = readBuffer.toString();
				logger.debug("Input received: " + scannedInput);
				//findStrings(scannedInput);
				parser.parse(scannedInput);
				
				inputStream.close();
			} catch (IOException e) {
			}

			break;
		}
	}
	
	private static final int GPGGA = 0;
	private static final int GPRMC = 1;
	private static final int GPGSA = 2;
	private static final int GPGSV = 3;
	
	private void findStrings(String string){
		int temp = strings.size();
		String type = string.substring(0, 6);
		strings.add(type);
		if(strings.size() != temp){
			System.out.println("Another one found");
			for(String s:strings){
				System.out.println(s);
			}
		}
		if(type.equals("$GPGGA")){
			count[GPGGA]++;
		}else if(type.equals("$GPRMC")){
			count[GPRMC]++;
		}else if(type.equals("$GPGSA")){
			count[GPGSA]++;
		}else if(type.equals("$GPGSV")){
			count[GPGSV]++;
		}else{
			System.out.println("Found another type: " + type);
		}
	}
}
